package com.he1618.boot3.config;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import com.nbsaas.boot.boot3.UserData;
import com.he1618.boot3.annotations.EncryptionData;
import com.nbsaas.boot.rest.annotations.CreateData;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

@RestControllerAdvice
public class RequestBodyDecodeAdvice extends RequestBodyAdviceAdapter {

	/**
	 * 128位的AESkey
	 */
	public static final byte[] AES_KEY = "+f0DrJTqGXg4BQNtBg1AWA==".getBytes(StandardCharsets.US_ASCII);

	@Override
	public boolean supports(MethodParameter methodParameter, Type targetType,
			Class<? extends HttpMessageConverter<?>> converterType) {
		/**
		 * 系统使用的是Gson作为json数据的Http消息转换器
		 */
		if (targetType instanceof Class<?>){
			CreateData allArgsConstructor=	((Class<?>)targetType).getAnnotation(CreateData.class);
			if (allArgsConstructor!=null){
				System.out.println("采用Lombok");
			}
			EncryptionData encryptionData=	methodParameter.getParameterAnnotation(EncryptionData.class);
			if (encryptionData!=null){
				return true;
			}else{
				return false;
			}

		}

		return GsonHttpMessageConverter.class.isAssignableFrom(converterType);
	}

	@Override
	public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
			Class<? extends HttpMessageConverter<?>> converterType) throws IOException {

		// 读取加密的请求体
		byte[] body = new byte[inputMessage.getBody().available()];
		inputMessage.getBody().read(body);

		try {
			// 使用AES解密
			body = this.decrypt(Base64.getDecoder().decode(body), AES_KEY);
		} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		// 使用解密后的数据，构造新的读取流
		InputStream rawInputStream = new ByteArrayInputStream(body);
		return new HttpInputMessage() {
			@Override
			public HttpHeaders getHeaders() {
				return inputMessage.getHeaders();
			}

			@Override
			public InputStream getBody() throws IOException {
				return rawInputStream;
			}
		};
	}
	public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
		if (body instanceof UserData){
			UserData data = (UserData) body;
			data.setAge(199);
		}
		return body;
	}
	/**
	 * AES解密
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public byte[] decrypt(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException,
			NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher = getCipher(key, Cipher.DECRYPT_MODE);
		return cipher.doFinal(data);
	}

	public byte[] encrypt(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException,
			NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher = getCipher(key, Cipher.ENCRYPT_MODE);
		return cipher.doFinal(data);
	}

	private Cipher getCipher(byte[] key, int model)
			throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
		SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(model, secretKeySpec);
		return cipher;
	}
}